package com.fg.cloud.manager.service.product.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.product.ProductSpecKeyEntity;
import com.fg.cloud.manager.entry.product.ProductSpecModelEntity;
import com.fg.cloud.manager.entry.product.vo.ProductSpecKeyVo;
import com.fg.cloud.manager.entry.product.vo.ProductSpecModelVo;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.product.ProductSpecValueMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.product.ProductSpecModelService;
import com.google.common.collect.Lists;

/**
  * @Description:商品规格模型接口实现
  * @author dingjiang
  * @date 2017年8月7日下午3:16:30
*/
@Service
public class ProductSpecModelServiceImpl extends BaseServiceImpl implements ProductSpecModelService {

	
	@Autowired
	private ProductSpecValueMapper SpecMapper ;
	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return SpecMapper;
	}
	
	/**
	 * Description:判断是否存在
	 * @param searchEntity
	 * @return
	 * @author dingjiang
	 * @date 2017年9月1日下午2:09:31
	 */
	public boolean judgeIsExist(ProductSpecModelEntity searchEntity) {
		
		return SpecMapper.judgeIsExist(searchEntity) > 0 ? true : false ;
	}
	
	public boolean JudgeIsExist(ProductSpecModelEntity searchEntity) {
		
		return SpecMapper.JudgeIsExist(searchEntity) > 0 ? true : false ;
	}
	
	/**
	 * 动态新增
	 */
	@Override
	@Transactional
	public boolean insert(ProductSpecModelEntity specModelEntity) {
		try {
			//动态新增规格模板
			int flag1 = SpecMapper.insertSelective(specModelEntity);
			//新增规格键值
			if ( null != specModelEntity.getSpecKeyEntities() ) {
				List<ProductSpecKeyEntity> keys = specModelEntity.getSpecKeyEntities();
				for (int i = 0; i < keys.size(); i++) {
					ProductSpecKeyEntity specKeyEntity = new ProductSpecKeyEntity();
					specKeyEntity.setSpecName(keys.get(i).getSpecName());
					specKeyEntity.setFacId(specModelEntity.getFacId());
					specKeyEntity.setModelId(specModelEntity.getId());
					SpecMapper.insertKey(specKeyEntity);
					if ( "[]" != keys.get(i).getValueList().toString() ) {
						SpecMapper.batchInsert(specModelEntity.getId(), specKeyEntity.getId(), keys.get(i).getValueList());
					}
				}
			}
			if ( flag1 > 0 ) return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}
	
	/**
	 * 新增规格键(值)
	 */
	@Override
	@Transactional
	public boolean insertKey(String specName, Long facId, Long modelId, List<String> valueList) {
		ProductSpecKeyEntity specKeyEntity = new ProductSpecKeyEntity();
		specKeyEntity.setSpecName(specName);
		specKeyEntity.setFacId(facId);
		specKeyEntity.setModelId(modelId);
		int flag1 = SpecMapper.insertKey(specKeyEntity);
		if ( valueList != null ) {
			int flag2 = SpecMapper.batchInsert(modelId, specKeyEntity.getId(), valueList);
			if ( flag1 > 0 && flag2 > 0 ) {
				return true;
			}
		}
		if ( flag1 > 0 ) {
			return true;
		}
		return false;
	}
	
	
	/**
	 * 分页查询
	 */
	@Override
	public void selectforPage(PageUtils<ProductSpecModelVo> pageinfo) {
		long total = SpecMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if (total>0) {
			List<ProductSpecModelVo> list = SpecMapper.findLists(pageinfo);
			List<ProductSpecModelVo> data = Lists.newArrayList();
			for (int i = 0; i < list.size() ; i++) {
				List<ProductSpecKeyVo> keys = list.get(i).getChildren();
				ProductSpecModelVo vo = new ProductSpecModelVo();
				
				if ( null != keys && keys.size() > 0 ) {
					StringBuffer sb = new StringBuffer();
					for (int j = 0; j < keys.size(); j++) {
						sb.append(keys.get(j).getLabel()+",");
					}
					String string = sb.toString();
					vo.setSpecKeys(string.substring(0, string.length()-1));
				}

				vo.setId( list.get(i).getId() );
				vo.setLabel( list.get(i).getLabel() );
				vo.setChildren(keys);
				data.add(vo);
			}
			pageinfo.setData(data);
		} else {
			pageinfo.setData(new ArrayList<ProductSpecModelVo>());
		}
	}
	
	/**
	 * 查询当前厂商的所有的模板
	 */
	@Override
	public List<ProductSpecModelVo> selectModels(Long facId) {
		
		return SpecMapper.selectModels(facId);
	}
	
	/**
	 * 根据ID查询
	 */
	@Override
	public ProductSpecModelVo find(Long id, Long facId) {

		return SpecMapper.find(id, facId);
	}
	
	
	
	/**
	 * 批量删除
	 */
	@Override
	@Transactional
	public boolean deletes(Long facId, Long[] ids) {
		try {
			//删除对应的规格键值
			int flag1 = SpecMapper.deleteValue(ids);
			int flag2 = SpecMapper.deleteKey(ids, facId);
			//删除规格模板
			int flag3 = SpecMapper.deleteModel(ids, facId);
			if ( flag1 > 0 && flag2 > 0 && flag3 >0 ) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}

	/**
	 * 删除规格键
	 */
	@Override
	public boolean remove(ProductSpecModelEntity specModelEntity) {
		List<ProductSpecKeyEntity> list = specModelEntity.getSpecKeyEntities();
		Long facId = specModelEntity.getFacId();
		Long[] keyIds = new Long[list.size()];
		for( int i=0; i<list.size(); i++ ){
			if ( StringUtils.isEmpty( list.get(i).getSpecName() ) ) {
				keyIds[i] = list.get(i).getId();
			}
		}
		int flag1 = SpecMapper.deleteKeys(keyIds, facId);
		int flag2 = SpecMapper.deleteValues(keyIds);
		if ( flag1>0 && flag2>0 ) {
			return true;
		}
		return false;
	}

	
	/**
	 * Description:修改规格
	 * @param specModelEntity
	 * @return
	 * @author dingjiang
	 * @date 2017年9月11日下午5:58:00
	 */
	@Override
	public boolean update(ProductSpecModelEntity specModelEntity) {
		//1.模板名修改
		int flag1 = SpecMapper.update(specModelEntity.getId(), specModelEntity.getModelName());
		int flag2 = 0;
		//2.规格键修改
		List<ProductSpecKeyEntity> list = specModelEntity.getSpecKeyEntities();
		for( int i=0; i<list.size(); i++ ){
			ProductSpecKeyEntity key = list.get(i);
			if ( !LongUtils.isEmpty(key.getId()) && !StringUtils.isEmpty(key.getSpecName()) ) {
				flag2 = SpecMapper.updateKey(key.getId(), key.getSpecName());
			}
		}
	    if ( flag1 > 0 && flag2 > 0 ) {
			return true;
		}
		return false;
	}
	
	@Override
	public boolean updateKey(ProductSpecKeyEntity key) {
		int flag = 0;
		if ( !LongUtils.isEmpty(key.getId()) && !StringUtils.isEmpty(key.getSpecName()) ) {
			flag = SpecMapper.updateKey(key.getId(), key.getSpecName());
		}
		if ( flag > 0 ) {
			return true;
		}
		return false;
	}
	
	/**
	 * 批量更新
	 */
	@Override
	@Transactional
	public boolean updateBatch(Long mid, Long id, List<String> spec) {
		int result = 0;
		//所谓批量更新就是先全部删除然后批量新增
		if ( !LongUtils.isEmpty(id) ) {
			int flag = SpecMapper.deleteAll(id);
			if ( flag >= 0 ) {
				result = SpecMapper.batchInsert(mid, id, spec);
			}
		}
		if ( result > 0 ) {
			return true;
		}
		return false;
	}

	/**
	 * 编辑保存
	 */
	@Override
	@Transactional
	public boolean modify(ProductSpecModelEntity specModelEntity) {
		//获取规格键集合
		List<ProductSpecKeyEntity> list = specModelEntity.getSpecKeyEntities();

		if ( !LongUtils.isEmpty(specModelEntity.getId()) ) {
			//1.规格模板名的编辑保存
			SpecMapper.update(specModelEntity.getId(), specModelEntity.getModelName());
			//2.规格key的编辑保存
			if ( null != list && list.size() > 0  ) {
				for( int i = 0; i < list.size(); i++ ) {
					ProductSpecKeyEntity key = list.get(i);
					//2.1 规格key的specName为null,则为规格删除
					if ( !LongUtils.isEmpty( key.getId()) && StringUtils.isEmpty(key.getSpecName()) ) {
						remove(specModelEntity);
					}
					//2.2 规格key的id为null,则为规格新增
					if ( LongUtils.isEmpty( key.getId() ) && !StringUtils.isEmpty( key.getSpecName() ) ) {
						if ( key.getValueList().toString() == "[]" ) {
							insertKey( key.getSpecName(), specModelEntity.getFacId(), specModelEntity.getId(), 
									null );
						} else {
							insertKey( key.getSpecName(), specModelEntity.getFacId(), specModelEntity.getId(),
									key.getValueList() );
						}
					}
					//2.3 规格key的id和specName都不为null,则为规格修改
					if ( !LongUtils.isEmpty(key.getId()) && !StringUtils.isEmpty(key.getSpecName()) && 
							key.getValueList() != null) {
						updateKey(key);
						updateBatch(specModelEntity.getId(), key.getId(), key.getValueList());
					}
				}
			}
		}
		return true;
	}

	@Override
	public ProductSpecModelEntity selectModelByName(Long facId, String modelName) {
		return SpecMapper.selectModelByName(facId, modelName);
	}

	

}
